package com.augmentum.login.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.augmentum.login.AppContent;
import com.augmentum.login.Constants;
import com.augmentum.login.exception.ParamException;
import com.augmentum.login.exception.ServiceException;
import com.augmentum.login.model.User;
import com.augmentum.login.service.UserService;
import com.augmentum.login.util.PathUtil;
import com.augmentum.login.util.StringUtil;

@Controller("userController")
@RequestMapping("/page/user")
public class UserController extends BaseController {
    private final Logger logger = Logger.getLogger(QuestionController.class);
    @Resource
    private UserService userService;

    @RequestMapping("/login")
    public ModelAndView login(@RequestParam(value = "userName", defaultValue = "") String userName,
            @RequestParam(value = "password", defaultValue = "") String password,
            @RequestParam(value = "go", defaultValue = "") String go,
            @RequestParam(value = "queryStr", defaultValue = "") String queryStr) {
        User user = null;
        ModelAndView view = new ModelAndView();
        try {
            user = userService.getUser(userName, password);
            user.setPassword(null);
            addSession(Constants.USER, user);
            if (StringUtil.isEmpty(go)) {
                view.setView(getRedirectView(Constants.SEARCH_QUESTION_SERVLET));
            } else {
                if (!StringUtil.isEmpty(queryStr)) {
                    if (queryStr.startsWith("#")) {
                        go += "?" + queryStr.substring(1);
                    }
                }
                view.setView(new RedirectView(AppContent.getContentPath() + "/" + go));
            }
        } catch (ParamException e) {
            logger.warn(e.getMessage());
            addSession(Constants.EXCEPTION, e.getExceptionFields());
            addSession(Constants.USER_NAME, userName);
            addSession(Constants.PASSWORD, password);
            view.setView(getRedirectView(Constants.LOGIN_PAGE_SERVLET));
        } catch (ServiceException e) {
            logger.warn(e.getMessage());
            addSession(Constants.USER_NAME, userName);
            addSession(Constants.PASSWORD, password);
            addSession(Constants.EXCEPTION, e.getMessage());
            view.setView(getRedirectView(Constants.LOGIN_PAGE_SERVLET));
        }
        return view;
    }

    @RequestMapping("/loginPage")
    public ModelAndView loginPage() {
        ModelAndView modelView = new ModelAndView();
        modelView.setViewName(Constants.LOGIN);
        return modelView;
    }

    @RequestMapping("/changePassword")
    public ModelAndView changePasswordPage() {
        ModelAndView modelView = new ModelAndView();
        modelView.setViewName(Constants.CHANGE_PASSWORD_PAGE);
        return modelView;
    }

    @RequestMapping("/delete")
    public ModelAndView delete(Map<String, Object> request, Map<String, Object> session) {
        return null;
    }

    @RequestMapping("/search")
    public ModelAndView search(Map<String, Object> request, Map<String, Object> session) {
        return null;
    }

    @RequestMapping("/edit")
    public ModelAndView edit(@RequestParam(value = "name", defaultValue = "") String name,
            @RequestParam(value = "password", defaultValue = "") String password,
            @RequestParam(value = "sex", defaultValue = "") String sex,
            @RequestParam(value = "telephone", defaultValue = "") String telephone,
            @RequestParam(value = "address", defaultValue = "") String address,
            @RequestParam(value = "role", defaultValue = "") int role,
            @RequestParam(value = "email", defaultValue = "") String email,
            @RequestParam(value = "chineseName", defaultValue = "") String chineseName) {
        User user = new User();
        user.setName(name);
        user.setTelephone(telephone);
        user.setEmail(email);
        user.setPassword(password);
        user.setAddress(address);
        user.setSex(sex);
        user.setRole(role);
        user.setChineseName(chineseName);
        userService.modifyUser(user);

        ModelAndView modelView = new ModelAndView();
        modelView.setView(getRedirectView(PathUtil.getFullPath(Constants.USER_SEARCH_SERVLET)));
        return modelView;
    }

    @RequestMapping("/personInfor")
    public ModelAndView personInfor(@RequestParam(value = "name") String name) {
        ModelAndView view = new ModelAndView();
        view.setViewName(Constants.PERSON_INFOR_JSP);
        return view;
    }

    @RequestMapping("/editPassword")
    public ModelAndView editPassword(@RequestParam(value = "id", defaultValue = "0") int id,
            @RequestParam(value = "password", defaultValue = "") String password) {
        userService.modifyPassword(id, password);
        ModelAndView modelView = new ModelAndView();
        modelView.setView(getRedirectView(Constants.PERSON_INFOR));
        return modelView;
    }

    @RequestMapping("/logout")
    public ModelAndView logout() {
        removeSession(Constants.USER);
        ModelAndView modelView = new ModelAndView();
        modelView.setView(getRedirectView(Constants.LOGIN_PAGE_SERVLET));
        return modelView;
    }
}
